home *** CD-ROM | disk | FTP | other *** search
- Path: wkaufman.us.oracle.com!wkaufman
- From: wkaufman@wkaufman.us.oracle.com (William Kaufman)
- Newsgroups: comp.lang.c
- Subject: Re: REPOST: Newbie Question -- byte order
- Date: 6 Mar 1996 06:52:10 GMT
- Organization: Oracle Corporation, Redwood Shores CA
- Message-ID: <4hjcmq$678@inet-nntp-gw-1.us.oracle.com>
- References: <4hg3u1$8d6@nnrp1.news.primenet.com>
- NNTP-Posting-Host: wkaufman.us.oracle.com
-
- In article <4hg3u1$8d6@nnrp1.news.primenet.com> piersen@primenet.com (Mark Cederholm) writes:
- ]
- ] I have found Turbo C++ a good package for developing data conversion routines,
- ] but recently I encountered a data concept not covered in their online
- ] documentation (or at least I can't find it):
- ]
- ] byte order (i.e. "bigendian" vs. "littleendian").
- ]
- ] What is it?
-
- Little-endian machines (like Intel/MS-DOS machines) order bytes from
- the least-significant to the most significant; big-endian machine (most
- other machines) order them the other way.
-
- So, for instance, assuming sizeof(short) == 2, this program
-
- #include <stdio.h>
-
- int main()
- {
- short x = 0x1234;
- char *c = (char *)&x;
-
- printf("0x%02x, 0x%02x\n", c[0], c[1]);
- return 0;
- }
-
- will print "0x12 0x34" on a big-endian machine, or "0x34 0x12" on a
- little-endian machine.
-
- So, if you're planning to move binary data between machines, you
- should pick either a big-endian or little-endian format and write out
- the data byte-by-byte (at least when you're on the wrong endian
- machine). Most sockets libraries include ntohs(), htons(), etc., to do
- this for you (sockets use big-endian format).
-
- ] Are there any good books or Web/Gopher resources that discuss it?
-
- Dunno, but you could always try Alta Vista at
- http://altavista.digital.com/.
-
- -- Bill K.
-
- Bill Kaufman | " While not a master of intellect, the blatantly
- wkaufman@us.oracle.com | obvious things we take for granted never
- | escape his keen eye! " -- Bob Burden
-